﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YiSha.Entity.RedBookSetManage;
using YiSha.Model.Param.RedBookSetManage;
using YiSha.Util.Extension;
using YiSha.Data.Repository;
using System.Linq;
using YiSha.Util.Model;
using System.Data.Common;
using YiSha.Data;
using YiSha.Util;
using YiSha.Entity.OrganizationManage;

namespace YiSha.Service.RedBookSetManage
{
    public class SysConsumeService : RepositoryFactory
    {
        #region 获取数据
        //

        public async Task<List<SysConsumeEntity>> GetTodayConsumeBySetIdList(long? redBookSetId)
        {
            var list=await this.BaseRepository().FindList<SysConsumeEntity>(string.Format("select * from SysConsume where DateDiff(dd,BaseCreateTime,getdate())=0  and RedBookSetId={0}", redBookSetId));
            return list.ToList();
        }
        public async Task<List<SysConsumeEntity>> GetTotalConsumeList(long? userId)
        {
            var list = await this.BaseRepository().FindList<SysConsumeEntity>(string.Format("select * from SysConsume where UserId={0}", userId));
            return list.ToList();
        }
        #endregion

        #region 提交数据

        public async Task SaveForm(SysConsumeEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert<SysConsumeEntity>(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update<SysConsumeEntity>(entity);
            }
        }

        public async Task SaveFormTrans(SysConsumeEntity sysConsumeEntity, UserEntity userEntity, RedBookSetEntity redBookSetEntity)
        {
            var db = await this.BaseRepository().BeginTrans();
            try
            {
                if (sysConsumeEntity.Id.IsNullOrZero())
                {
                    await sysConsumeEntity.Create();
                    await this.BaseRepository().Insert<SysConsumeEntity>(sysConsumeEntity);
                }
                else
                {
                    await  sysConsumeEntity.Modify();
                    await db.Update<SysConsumeEntity>(sysConsumeEntity);
                }
                await  userEntity.Modify();
                await db.Update<UserEntity>(userEntity);
                await redBookSetEntity.Modify();
                await db.Update(redBookSetEntity);

                await db.CommitTrans();
            }
            catch
            {
                await db.RollbackTrans();
                throw;
            }
        }
        #endregion

    }
}
